package org.example

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

object Scala03 {

  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.ERROR)
    //初始化
    println("=====初始化=======")
    val conf = new SparkConf().setAppName("Scala03").setMaster("local")
    val sc = new SparkContext(conf)
    val sQLContext = new SQLContext(sc)
    val df = sQLContext.read.json("/Users/sungengjia/Desktop/People.json")
    val df_price = sQLContext.read.json("/Users/sungengjia/Desktop/Price.json")
    println("======引入json数据=======")
    println("======查看json数据=======")
    df.show()
    //查看关联后的结算
    val df_join: DataFrame = df_price.join(df,df("id")===df_price("errNO"))
    df_join.show()

    println("======关联后结果========")
    val df_left_join = df_price.join(df,df("id")===df_price("errNO"),"left")
    df_left_join.show()

    println("=====jointype结果======")
    df.printSchema()
    df.filter(df.col("age").gt(32)).show()
    df.select(df.col("name"),df.col("age").plus(1)).show()
    df.groupBy(df.col("age")).count().show()
    println("=======使用Lambda表达式的结果======")
    df.select(df.col("name"),df.col("age"),df.col("id")).foreach(x=>{
      println(x)
    })
  }

}
package org.example

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

object Scala03 {

  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.ERROR)
    //初始化
    println("=====初始化=======")
    val conf = new SparkConf().setAppName("Scala03").setMaster("local")
    val sc = new SparkContext(conf)
    val sQLContext = new SQLContext(sc)
    val df = sQLContext.read.json("/Users/sungengjia/Desktop/People.json")
    val df_price = sQLContext.read.json("/Users/sungengjia/Desktop/Price.json")
    println("======引入json数据=======")
    println("======查看json数据=======")
    df.show()
    //查看关联后的结算
    val df_join: DataFrame = df_price.join(df,df("id")===df_price("errNO"))
    df_join.show()

    println("======关联后结果========")
    val df_left_join = df_price.join(df,df("id")===df_price("errNO"),"left")
    df_left_join.show()

    println("=====jointype结果======")
    df.printSchema()
    df.filter(df.col("age").gt(32)).show()
    df.select(df.col("name"),df.col("age").plus(1)).show()
    df.groupBy(df.col("age")).count().show()
    println("=======使用Lambda表达式的结果======")
    df.select(df.col("name"),df.col("age"),df.col("id")).foreach(x=>{
      println(x)
    })
  }

}
